package gov.kqqzyyy.monkey.pharmacyscreen.code.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import gov.kqqzyyy.monkey.pharmacyscreen.R;
import gov.kqqzyyy.monkey.pharmacyscreen.code.mytools.MyStringUtil;
import gov.kqqzyyy.monkey.pharmacyscreen.code.object.Patient;

/**
 * Created by Monkey on 2016/1/22.
 * gridview适配器
 */
public class MyGridViewAdapter extends BaseAdapter {

    private final Context mContext;
    private List<Patient> patientList;
    private final LayoutInflater inflater;

    private int textSize = 40;
    private int textColor;

    private int PDHM = -1;
    private boolean changeTypeFace = true;

    public void setChangeTypeFace(boolean changeTypeFace) {
        this.changeTypeFace = changeTypeFace;
    }

    public MyGridViewAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        textColor = ContextCompat.getColor(mContext, R.color.colorBlack);
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setPatientList(List<Patient> list) {
        this.patientList = removeDuplicate(list);
        notifyDataSetChanged();
    }

    public void setPDHM(int PDHM) {
        this.PDHM = PDHM;
        notifyDataSetChanged();
    }

    public List<Patient> getPatientList() {
        return patientList;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    @Override
    public int getCount() {
        if (patientList != null) {
            return patientList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (patientList != null && patientList.get(position) != null) {
            return patientList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return Math.max(position, 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_pharmacy_patient, parent, false);
            holder.patientName = convertView.findViewById(R.id.patient_name_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (patientList != null && patientList.get(position) != null) {
            int thisColor;
            if (patientList.get(position).getPDHM() == PDHM) {
                thisColor = ContextCompat.getColor(mContext, R.color.colorOrange);
            } else {
                thisColor = textColor;
            }
            if (changeTypeFace) {
                if (patientList.get(position).getMJBZ() == 1) {
                    thisColor = ContextCompat.getColor(mContext, R.color.color_klj_blue);
                }
                if (patientList.get(position).getBRNL() >= 80) {
                    holder.patientName.setTypeface(holder.patientName.getTypeface(), Typeface.BOLD_ITALIC);
                }
            }
            holder.patientName.setTextColor(thisColor);

            String text = MyStringUtil.doFormatXH(String.valueOf(patientList.get(position).getPDHM())) + MyStringUtil.doHideName(patientList.get(position).getBRXM());
            holder.patientName.setText(text);
            holder.patientName.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView patientName;
    }

    //去除重复项
    private List<Patient> removeDuplicate(List<Patient> list) {
        Set<Patient> set = new HashSet<>(list);
        List<Patient> newList;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            newList = new ArrayList<>(set);
        } else {
            newList = new ArrayList<>(set);
        }
        Collections.sort(newList, (arg0, arg1) -> arg0.getPDHM() - arg1.getPDHM());
        return newList;
    }
}
